﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Datorgrafik_lab1.Models {
    class Cube
    {
        private VertexPositionColor[] vertices;
        private short[] indices;
        VertexBuffer cubeVB;

        public VertexBuffer CubeVB
        {
            get { return cubeVB; }
            set { cubeVB = value; }
        }
        IndexBuffer cubeIB;

        public IndexBuffer CubeIB
        {
            get { return cubeIB; }
            set { cubeIB = value; }
        }
        GraphicsDevice graphicsDevice;

        private const int FTR = 0;
        private const int FTL = 1;
        private const int FBR = 2;
        private const int FBL = 3;

        private const int BTR = 4;
        private const int BTL = 5;
        private const int BBR = 6;
        private const int BBL = 7;

        public float SizeX;
        public float SizeY;
        public float SizeZ;
        public Vector3 Position;
        Color color;

        public Cube(GraphicsDevice graphicsDevice, Vector3 position, float sizeX, float sizeY, float sizeZ, Color modelColor)
        {
            this.graphicsDevice = graphicsDevice;

            Position = position;
            SizeX = sizeX;
            SizeY = sizeY;
            SizeZ = sizeZ;
            color = modelColor;

            setupModel();
        }

        private void setupModel() {
            InitializeCubeVertices();
            InitializeCubeIndices();

            cubeVB = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
            cubeIB = new IndexBuffer(graphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);

            cubeVB.SetData<VertexPositionColor>(vertices);
            cubeIB.SetData<short>(indices);
        }


        public void Draw()
        {
            graphicsDevice.SetVertexBuffer(cubeVB);
            graphicsDevice.Indices = cubeIB;
            graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
        }

        private void InitializeCubeVertices()
        {
            vertices = new VertexPositionColor[8];

            //Front Top right
            vertices[FTR] = new VertexPositionColor(new Vector3(Position.X + SizeX, Position.Y + SizeY, Position.Z + SizeZ), color);
            //Front top left
            vertices[FTL] = new VertexPositionColor(new Vector3(Position.X - SizeX, Position.Y + SizeY, Position.Z + SizeZ), color);
            //Front bottom right
            vertices[FBR] = new VertexPositionColor(new Vector3(Position.X + SizeX, Position.Y, Position.Z + SizeZ), color);
            //Front bottom left
            vertices[FBL] = new VertexPositionColor(new Vector3(Position.X - SizeX, Position.Y, Position.Z + SizeZ), color);
            //Back Top right
            vertices[BTR] = new VertexPositionColor(new Vector3(Position.X + SizeX, Position.Y + SizeY, Position.Z - SizeZ), color);
            //Back top left
            vertices[BTL] = new VertexPositionColor(new Vector3(Position.X - SizeX, Position.Y + SizeY, Position.Z - SizeZ), color);
            //Back bottom right
            vertices[BBR] = new VertexPositionColor(new Vector3(Position.X + SizeX, Position.Y, Position.Z - SizeZ), color);
            //Back bottom left
            vertices[BBL] = new VertexPositionColor(new Vector3(Position.X - SizeX, Position.Y, Position.Z - SizeZ), color);

        }

        private void InitializeCubeIndices()
        {
            //6 vertices make up 2 triangles which make up our rectangle
            indices = new short[36];

            //Front triangle 1 (bottom portion) |\
            indices[0] = FTL; //top left;
            indices[1] = FBR; // bottom right;
            indices[2] = FBL; // bottom left;

            //Front Triangle 2 (top portion) \|
            indices[3] = FTL; // top left;
            indices[4] = FTR; // top right;
            indices[5] = FBR; // bottom right;

            //Back triangle 1 (bottom portion) |\
            indices[6] = BTL; //top left;
            indices[8] = BBR; // bottom right;
            indices[7] = BBL; // bottom left;

            //Back Triangle 2 (top portion) \|
            indices[9] = BTL; // top left;
            indices[11] = BTR; // top right;
            indices[10] = BBR; // bottom right;

            //LEFT triangle 1 (bottom portion) |\
            indices[12] = BTL; //top left;
            indices[13] = FBL; // bottom right;
            indices[14] = BBL; // bottom left;

            //LEFT Triangle 2 (top portion) \|
            indices[15] = BTL; // top left;
            indices[16] = FTL; // top right;
            indices[17] = FBL; // bottom right;

            //RIGHT triangle 1 (bottom portion) |\
            indices[18] = BTR; //top left;
            indices[20] = FBR; // bottom right;
            indices[19] = BBR; // bottom left;

            //RIGHT Triangle 2 (top portion) \|
            indices[21] = BTR; // top left;
            indices[23] = FTR; // top right;
            indices[22] = FBR; // bottom right;

            //TOPP triangle 1 (bottom portion) |\
            indices[24] = BTL; //top left;
            indices[25] = FTR; // bottom right;
            indices[26] = FTL; // bottom left;

            //TOPP Triangle 2 (top portion) \|
            indices[27] = BTL; // top left;
            indices[28] = BTR; // top right;
            indices[29] = FTR; // bottom right;

            //BOTTOM triangle 1 (bottom portion) |\
            indices[30] = BBL; //top left;
            indices[32] = FBR; // bottom right;
            indices[31] = FBL; // bottom left;

            //BOTTOM Triangle 2 (top portion) \|
            indices[33] = BBL; // top left;
            indices[35] = BBR; // top right;
            indices[34] = FBR; // bottom right;
        }
    }
}
